<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Service\ChCustomerServiceLinkService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Mine\Exception\NormalStatusException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'ch/attract/customerServiceLink'),ApiAuth,VerifySignature]
class ChCustomerServiceLinkController extends BaseController
{
    public array $noNeedLogin = ['randomQrcode'];
    public array $noNeedVerifySign = ['randomQrcode'];

    #[Inject]
    protected ChCustomerServiceLinkService $service;

    /**
     * 链接列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('linkList')]
    public function getLinkList(): ResponseInterface
    {
        return $this->success($this->service->getLinkList($this->request->all()));
    }

    /**
     * 单个链接的机器人列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('robotListByLink')]
    public function getCustomerServiceListByLink(): ResponseInterface
    {
        return $this->success($this->service->getRobotListByLink($this->request->all()));
    }

    /**
     * 增加链接
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function saveLink(): ResponseInterface
    {
        $check = $this->service->checkRepeat($this->request->input('uid'),$this->request->input('name'));
        if ($check) {
            throw new NormalStatusException('请不要输入重复名称');
        }
        return $this->success(['id' => $this->service->saveLink($this->request->all())]);
    }

    /**
     * 更新链接
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PutMapping('update')]
    public function updateLink(): ResponseInterface
    {
        $check = $this->service->checkRepeat($this->request->input('uid'),$this->request->input('name'),$this->request->input('id'));
        if ($check) {
            throw new NormalStatusException('该名称已被其他链接使用');
        }
        return $this->service->updateLink($this->request->all())?$this->success():$this->error();
    }

    /**
     * 删除链接
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping('delete')]
    public function deleteLink()
    {
        return $this->service->deleteLink($this->request->all())?$this->success():$this->error();
    }

    /**
     * 随机展示机器人二维码
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping('randomQrcode')]
    public function randomQrcode(): ResponseInterface
    {
        return $this->success(['Qrcode' => $this->service->randomQrcode($this->request->all())]);
    }

    /**
     * 已被选入链接的机器人ID
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('usedRobotIds')]
    public function usedRobotIds(): ResponseInterface
    {
        return $this->success($this->service->usedRobotIds($this->request->all()));
    }

    /**
     * 链接详情
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('linkDetail')]
    public function getLinkDetail(): ResponseInterface
    {
        return $this->success($this->service->getLinkDetail($this->request->all()));
    }

    /**
     * 更改是否使用新客户运营状态状态
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('changeStatus')]
    public function changeStatus(): ResponseInterface
    {
        return $this->service->changeStatus($this->request->input('link_id'),$this->request->input('status'),'is_use_new_customer_scheme')?$this->success():$this->error();
    }
}